package com.mewpipe.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import com.mewpipe.dao.VideoDao;
import com.mewpipe.entity.Video;


public class JpaVideoDao implements VideoDao {

	private EntityManagerFactory emf;
	


	public JpaVideoDao(EntityManagerFactory emf) {
	
			this.emf = emf;
		
			}

	@Override
	public Long addVideo(Video _video) {
		
		EntityManager em = emf.createEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(_video);
			em.getTransaction().commit();
		
			em.refresh(_video);
			return _video.getId();
		} 
		
		
		finally {
			if(em.getTransaction().isActive()) em.getTransaction().rollback();
			em.close();
		
		}
		
	}

	@Override
	public void deleteVideoById(long _id) {
		
	EntityManager em=emf.createEntityManager();
		
		try {
			em.getTransaction().begin();
			
			Video videotoremove = getVideoInfo(_id);
			// On merge pour recuperer l'instance de la video dans la MEME transaction que le REMOVE !!!
			videotoremove = em.merge(videotoremove);
			
			em.remove(videotoremove);
			em.getTransaction().commit();
			
		} 
		
		
		finally {
			if(em.getTransaction().isActive()) em.getTransaction().rollback();
			em.close();
		
		}
		
		
	}

	@Override
	public Video getVideoInfo(long _id) {
		
		Video return_video ;
		EntityManager em = emf.createEntityManager();
		try {
			em.getTransaction().begin();
			return_video = em.find(Video.class,_id);
			em.getTransaction().commit();
			
		} 
		
		
		finally {
			if(em.getTransaction().isActive()) em.getTransaction().rollback();
			em.close();
		
		}
		
			return return_video;
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> getVideoByUserId(long _id) {
		
		List<Video> return_list= null ;
		
		EntityManager em = emf.createEntityManager();
	
		
		Query query= em.createQuery("SELECT VideoTable FROM Video VideoTable ,LnkUserVideo LinkTable WHERE VideoTable.id = LinkTable.VideoID AND LinkTable.UserID="+ _id);
		//Query query = em.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").addEntity(Video.class);
		try {			
			return_list = (List<Video>) query.getResultList();
			} 
		
		finally {
			em.close();
				}
		return return_list;
			}

	@Override
	public void updateVideo(Video _video) {
		
			
			EntityManager em = emf.createEntityManager();
			
			
			try {
				
	            em.getTransaction().begin();
	            em.merge(_video);
	            em.getTransaction().commit();
				
			} finally {
				
				em.close();
			}
		
		
		
	}

	@Override
	public String getVideoIdByCode(String code) {
		
		
		EntityManager em = emf.createEntityManager();
	
		
		Query query= em.createQuery("SELECT id FROM Video WHERE idShare ='"+ code+"'");
		Object idvideo;
		//Query query = em.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").addEntity(Video.class);
		try {			
			List idvideo2 =  query.getResultList();
			idvideo = idvideo2.get(0);
			} 
		
		finally {
			em.close();
				}
		return String.valueOf(idvideo);
	}

	@Override
	public List<Video> getindexvideo() {
EntityManager em = emf.createEntityManager();
	
		
		Query query= em.createQuery("SELECT VideoTable FROM Video VideoTable ORDER BY  nbVue ASC");
		
		List<Video> videolist;
		
		try {			
			 videolist = (List<Video>) query.getResultList();
			
			} 
		
		finally {
			em.close();
				}
		return videolist;

		
	}

	@Override
	public String getVideoSearch(String nom) {
EntityManager em = emf.createEntityManager();
	
		
		Query query= em.createQuery("SELECT idShare FROM Video WHERE nom ='"+ nom+"'");
		String video;
		
		//Query query = em.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").addEntity(Video.class);
		try {			
			List listvideo =  query.getResultList();
			video = (String) listvideo.get(0);
			
			} 
		
		finally {
			em.close();
			
				}
		return  video;
	}
		
	
}
